package com.zzyl.nursing.controller.member;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.ReservationMember;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.IReservationMemberService;
import com.zzyl.nursing.vo.TimeCountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/member/reservation")
public class ReservationMemberController extends BaseController {

    @Autowired
    private IReservationMemberService reservationMemberService;
    @Autowired
    private IFamilyMemberService familyMemberService;
    /**
     * 获取预约取消次数
     *
     * @return
     */
    @GetMapping("/cancelled-count")
    private AjaxResult cancelledCount() {

        Integer sum = reservationMemberService.cancelledCount();
        return success(sum);
    }

    /**
     * 获取每个时间段预约数量
     *
     * @return
     */
    @GetMapping("/countByTime")
    private AjaxResult countByTime(Long time) {
        List<TimeCountVo> list = reservationMemberService.countByTime(time);
        return success(list);
    }

    @GetMapping("/page")
    public AjaxResult page(ReservationMember reservationMember){
        long id = UserThreadLocal.getUserId();
        reservationMember.setCreateBy(String.valueOf(id));
        startPage();
        List<ReservationMember> list = reservationMemberService.getPageList(reservationMember);
        return success(getDataTable(list));
    }

    @PutMapping("/{id}/cancel")
    public AjaxResult cannel(@PathVariable Long id){
        reservationMemberService.cannel(id);
        return success();
    }


    @PostMapping
    public AjaxResult add(@RequestBody ReservationDto dto){
        try {
            reservationMemberService.add(dto);
        }catch (Exception e){
            return error("您在当前时间段已预约");
        }
        return success();

    }

}
